DAMN = (typeof DAMN != "undefined")?  DAMN : {};

DAMN.Browser = new function() 
{   
  this.AttachTo = function(parent) {
    parent.tabs('add', '#tabs-Browser', "Browse");
    var tab = $("#tabs-Browser");
    
    //Extra child for scrollbars.
    var pa = $("<div></div>").css({height: '95%', overflow: 'hidden'});
    tab.append(pa);
    tab = pa;
        
    this.tree = $("<div></div>");   
    this.tree.css({height: '400px', overflow: 'auto'}); 
    tab.append(this.tree);
    
    var self = this;  

		this.tree.jstree({ 
      "core" : {"animation" : 0},
			// the list of plugins to include
			"plugins" : [ "themes", "json_data", "types", "ui"],
			// Plugin configuration
			"json_data" : { 
				"ajax" : {
					"url" : "/files/",
					"data" : function (n) { 
						return {  
							"fileid" : n.attr ? n.attr("fileid") : -1 
						}; 
					},
					"progressive_render" : true
				}
			},
			"types" : {
				"valid_children" : [ "root" ],
				"types" : {
					// The default type
					"default" : {
					  "valid_children" : "none",
						"start_drag" : false,
						"move_node" : false,
						"delete_node" : false,
						"remove" : false
					},
					// The `root` type
					"root" : {
						"valid_children" : [ "default", "folder", "file", "blend", "image" ],
						"icon" : {
							"image" : "/static/drive.png"
						}
					},
					// The `folder` type
					"folder" : {
						"valid_children" : [ "file", "blend", "image" ]
					},
					// The `drive` nodes 
					"file" : {
						"icon" : {
							"image" : "/static/file.png"
						}
					},
					"blend" : {
						"icon" : {
							"image" : "/static/blender.png"
						}
					},
					"image" : {
						"icon" : {
							"image" : "/static/image.png"
						}
					}
				}
			},
			"ui" : {
  			"select_limit" : 1
		  }

			
		});
		
		this.tree.bind("select_node.jstree", function (event, data){
		  if (!self.disableEvents && data.rslt.obj.attr('fileid'))
        	DAMN.AssetMananger.FireOnSelection(data.rslt.obj, 'file');
    });
    
    this.tree.bind("delect_node.jstree", function (event, data){
      if (!self.disableEvents && data.rslt.obj.attr('fileid'))
		    DAMN.AssetMananger.FireOnUnSelection(data.rslt.obj, 'file');
    });
    
    this.tree.bind("loaded.jstree", function (event, data){
		  this.loaded = true;
    });
		
  
    var self = this;		
    $('.go-to-asset').live('click', function() {
        DAMN.Log.debug("DAMN.Browser: live");
        self.Select($(this).attr('asset'));
		});
  };
  
  this.previous = null;
  
  this.FireOnChange = function(item) {
    DAMN.AssetMananger.ClearAll();
    if (this.previous != null) {
      DAMN.AssetMananger.FireOnUnSelection(this.previous);
    }
    DAMN.AssetMananger.FireOnSelection(item);
    this.previous = item;
	};
	
	this.FireOnLoad = function(cb) {
    var self = this;
	  if (this.loaded == true) {
      cb();
    }
	  else {
      this.tree.bind("loaded.jstree", function (event, data){
		    this.loaded = true;
		    cb();
      });
    }
	};
  
  this.Select = function(assetId) {
    var self = this;
  
    var Recurse = function (data, index) {
      if (index < data.length) {
        var el = $('li[fileid="'+data[index].pk+'"]');
        DAMN.Log.debug("DAMN.Browser: Recurse "+data[index].pk+", "+index+", "+el.length);
		    if (index < data.length-1) {
			    self.disableEvents = true;
	        $.jstree._focused().open_node(el, function() {
	          DAMN.Log.debug("DAMN.Browser: Recurse: "+index+" "+data[index].pk);
	          Recurse(data, index+1);
	        }, true);
     		} else {
			    self.disableEvents = false;
			    $.jstree._focused().select_node(el);
			    DAMN.AssetMananger.FireOnSelection($('<li assetid="'+assetId+'"/>'), 'asset');
		    }
      }
    };	
  
    $.getJSON("/assets/"+assetId+"/parents/", function(data){                         
      Recurse(data, 0);
      $(".tabs-closable").tabs('select', 0); //Show the Main tab
	  $("#ui-layout-west-tabs").tabs('select', 1); //Show the browse tab
    });	
  };			
};

